package com.viphuli.app.tool.fragment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Spinner;
import butterknife.Bind;

import com.loopj.android.http.RequestParams;
import com.offroader.utils.DateUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.bean.part.ArrangeViewStatistics;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.http.ApiRequest;
import com.viphuli.app.tool.utils.OtherUtils;

public class ArrangeViewMonthFragment extends BaseProgressFragment implements OnItemSelectedListener {

	//当前显示的周
	private int curWhich;

	@Bind(R.id.id_arrange_detail_week_time)
	protected Spinner spinner;

	@Bind(R.id.id_view_week_layout)
	protected ViewGroup viewWeekLayout;

	@Bind(R.id.id_listview)
	protected ListView listview;

	private long time;

	private Map<Integer, Date> data;

	private List<ArrangeViewStatistics> list = new ArrayList<ArrangeViewStatistics>();

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_month_view;
	}

	@Override
	protected void initParams() {

	}

	@Override
	protected void initView() {
		time = System.currentTimeMillis();
	}

	@Override
	protected void initData() {

		//顶部
		initTopWeekTime();

	}

	/**
	 * 初始化项部周列表
	 * @param time
	 */
	private void initTopWeekTime() {

		int nowYear = DateUtils.getYear(new Date(time));//哪年
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
		List<String> weekLineDataList = new ArrayList<String>();
		Date now = new Date(time);
		
		data = new HashMap<Integer, Date>();

		//前后三年的数据
		List<Integer> years = Arrays.asList(nowYear - 1, nowYear, nowYear + 1);

		int index = -1;

		for (int year : years) {

			int monthCountYear = 12;

			for (int i = 0; i < monthCountYear; i++) {//遍历月份

				index++;

				int month = i + 1;

				//当前信息
				List<Date> dayOfMonthDays = DateUtils.dateToMonth(year, month);
				data.put(index, dayOfMonthDays.iterator().next());

				int y = DateUtils.getYear(dayOfMonthDays.iterator().next());
				int m = DateUtils.getMonthInYear(dayOfMonthDays.iterator().next());

				if (y == DateUtils.getYear(now) && m == DateUtils.getMonthInYear(now)) {
					curWhich = index;
				}

				if (i == 0) {//当第一月时， 取本月最后一天的年月

					Date lastDateOfMonth = dayOfMonthDays.get(dayOfMonthDays.size() - 1);
					String yearMonth = sdf.format(lastDateOfMonth);

					String data = yearMonth;

					weekLineDataList.add(data);

				} else if (i == 0) {//当最后月时，取本月第一天的年月

					Date firstDateOfMonth = dayOfMonthDays.get(0);
					String yearMonth = sdf.format(firstDateOfMonth);

					String data = yearMonth;

					weekLineDataList.add(data);

				} else {//其它情况，当前月任意天即可

					Date firstDateOfMonth = dayOfMonthDays.get(0);
					String yearMonth = sdf.format(firstDateOfMonth);

					String data = yearMonth;

					weekLineDataList.add(data);

				}

			}

		}

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_arrange_view, weekLineDataList);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);

//		int selectWeek = DateUtils.getMonthInYear(new Date(time));
		spinner.setSelection(curWhich);

		spinner.setOnItemSelectedListener(this);
	}

	public void monthViewRequest(int year, int which) {
		curWhich = which;

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (tokenKeeper.isLogin()) {

			//当前月第一天
			List<Date> dayList = DateUtils.dateToMonth(year, which);
			long firstDayMonth = OtherUtils.maxLength(dayList.iterator().next().getTime());

			RequestParams params = new RequestParams();
			params.put("which", which);
			params.put("type", 2);
			params.put("uid", tokenKeeper.getOpenId());
			params.put("circle_id", tokenKeeper.getUser().circleId());
			params.put("time", firstDayMonth);

			//取月视图数据
			ApiRequest.arrangeMonthView.request(this, params);

		}
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

		Date date = data.get(position);

		int year = DateUtils.getYear(date);
		int month = DateUtils.getMonthInYear(date);

		monthViewRequest(year, month);

	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {

	}

	public ViewGroup getViewWeekLayout() {
		return viewWeekLayout;
	}

	public ListView getStatisticsListview() {
		return listview;
	}

	public List<ArrangeViewStatistics> getList() {
		return list;
	}

}
